home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / print / proff.zip / PROFF01.C < prev    next >
Text File  |  1988-02-13  |  13KB  |  760 lines

  1. #include <stdio.h>
  2. #include <ctype.h>
  3. #include <time.h>
  4. #include "proff.h"
  5. #include "debug.h"
  6.  
  7. /*
  8.  * bold - bold face or overstrike a line
  9.  *
  10.  */
  11. bold(buf,tbuf,size)
  12. char buf[];
  13. char tbuf[];
  14. int  size;
  15. {
  16.     int  i,j;
  17.     dprintf("bold  ");
  18.  
  19.     j = 0;
  20.     for (i = 0; buf[i] != '\n' && j < size - 2; i++) {
  21.         tbuf[j] = buf[i];
  22.         j++;
  23.         if (buf[i] != ' ' && buf[i] != '\t' &&
  24.             buf[i] != BACKSPACE ) {
  25.             tbuf[j] = BACKSPACE;
  26.             tbuf[j+1] = tbuf[j-1];
  27.             j += 2;
  28.         }
  29.     }
  30.     tbuf[j] = '\n';
  31.     tbuf[j+1] = '\0';
  32.     strcpy(buf,tbuf);
  33. }
  34.  
  35. /*
  36.  * lbrk - end current filled line
  37.  *
  38.  */
  39. lbrk()
  40. {
  41.     dprintf("lbrk  ");
  42.     if (outp > 0) {
  43.         outbuf[outp] = '\n';
  44.         outbuf[outp+1] = EOS;
  45.         put(outbuf);
  46.     }
  47.     outp = 0;
  48.     outw = 0;
  49.     outwds = 0;
  50. }
  51.  
  52. /*
  53.  * center - center a line by setting tival
  54.  *
  55.  */
  56. center(buf)
  57. char buf[];
  58. {
  59.     int    i;
  60.  
  61.     dprintf("center  ");
  62.  
  63.     i = (rmval + tival - width(buf)) / 2;
  64.     tival = (i > 0) ? i : 0;
  65. }
  66.  
  67. /*
  68.  * doroff - format text in file fp
  69.  *
  70.  */
  71. doroff(fp)
  72. FILE *fp;
  73. {
  74.     char inbuf[INSIZE];
  75.  
  76.     infile[0] = fp;
  77.     for (level = 0; level > -1; level--) {
  78. #ifdef DEBUG
  79.     printf("doroff fp = %d   level = %d   infile[level] = %d\n", fp, level, infile[level]);
  80. #endif
  81.         while (ngetln(inbuf, infile[level]) != EOF) {
  82. #ifdef DEBUG
  83.       printf("doroff fp = %d   level = %d   infile[level] = %d\n", fp, level, infile[level]);
  84. #endif
  85.             if (inbuf[0] == cchar)    /* a command */
  86.                 command(inbuf);
  87.             else {
  88. #ifdef rainbow
  89.                 if (biosb(2))
  90.                     exit(0);
  91. #endif
  92.                 text(inbuf);
  93.                 p_txtlines++;
  94.             }
  95.         }
  96.         if (level > 0 && infile[level] > 0) {
  97.             fclose(infile[level]);
  98.             if (verbose)
  99.                 fprintf(stderr,"       done.\n");
  100.         }
  101.     }
  102. }
  103.  
  104. /*
  105.  * gettl - copy title from buf to ttl
  106.  *
  107.  * modifies lim
  108.  */
  109. gettl(buf,ttl,lim)
  110. char *buf;
  111. char *ttl;
  112. int lim[];
  113. {
  114.     while (!isspace(*buf))
  115.         buf++;
  116.     while (isspace(*buf))
  117.         buf++;
  118.     strcpy(ttl,buf);
  119.     lim[0] = inval;
  120.     lim[1] = rmval;
  121. }
  122.  
  123. /*
  124.  * getwrb - get a word INCLUDING the trailing blanks
  125.  *
  126.  */
  127. int
  128. getwrb(in,i,out)
  129. char in[];
  130. char out[];
  131. int *i;
  132. {
  133.     int j,k;
  134.     dprintf("getval  ");
  135.     k = *i;
  136.     j = 0;
  137.     while (in[k] != EOS && in[k] != ' ' &&
  138.         in[k] != '\t' && in[k] != '\n') {
  139.         out[j] = in[k];
  140.         k++;
  141.         j++;
  142.     }
  143.     while (in[k] == ' ') {
  144.         out[j] = ' ';
  145.         k++;
  146.         j++;
  147.     }
  148.     *i = k;
  149.     out[j] = EOS;
  150.     return(j);
  151. }
  152.  
  153.  
  154. /*
  155.  * gfield - get next tab or title field
  156.  *
  157.  */
  158. int
  159. gfield(buf, i, n, temp, delim)
  160. char buf[];
  161. int *i;
  162. int n;
  163. char temp[];
  164. char delim;
  165. {
  166.     int j,k;
  167.  
  168.     dprintf("gfield  ");
  169.     j = 0;
  170.     k = *i;
  171.     if (n > 0) {
  172.         if (buf[k] == delim)
  173.             k++;
  174.         while (buf[k] != delim && buf[k] != EOS && buf[k] != '\n' &&
  175.             j <= n) {
  176.             temp[j] = buf[k];
  177.             j++;
  178.             k++;
  179.         }
  180.     }
  181.     temp[j] = EOS;
  182.     while (buf[k] != delim && buf[k] != EOS && buf[k] != '\n')
  183.         k++;
  184.     *i = k;
  185.     return(j);
  186. }
  187.  
  188. /*
  189.  * jcopy - scopy without copying EOS
  190.  *
  191.  */
  192. jcopy(from, i, to, j)
  193. char from[];
  194. char to[];
  195. int i;
  196. int j;
  197. {
  198.     int k1, k2;
  199.     dprintf("jcopy  ");
  200.  
  201.     k1 = i;
  202.     k2 = j;
  203.     while (from[k1] != EOS) {
  204.         to[k2] = from[k1];
  205.         k1++;
  206.         k2++;
  207.     }
  208. }
  209.  
  210. /*
  211.  * justfy - justifies string in its tab column
  212.  * */
  213. justfy(in, left, right, type, out)
  214. char in[];
  215. char out[];
  216. int left;
  217. int right;
  218. int type;
  219. {
  220.     int j,k, n;
  221.  
  222.     dprintf("justfy  ");
  223.     n = width(in);
  224.     if (type == RIGHT)
  225.         jcopy(in, 0, out, right-n);
  226.     else if (type == CENTER) {
  227.         k = (right+left-n) / 2;
  228.         j = (k > left) ? k : left;
  229.         jcopy(in, 0, out, j);
  230.     }
  231.     else 
  232.         jcopy(in, 0, out, left);
  233. }
  234.  
  235. /*
  236.  * leadbl - delete leading blanks, set tival
  237.  *
  238.  */
  239. leadbl(buf)
  240. char buf[];
  241. {
  242.     int i, j;
  243.  
  244.     dprintf("leadbl  ");
  245.     lbrk();
  246.     for (i = 0; buf[i] == ' '; i++)     /* find 1st non-blank */
  247.         ;
  248.     if (buf[i] != '\n')
  249.         if (autopar) {
  250.             put("\n");    /* blank line */
  251.             tival = inval + autoprv;
  252.         }
  253.         else
  254.             tival = inval + i;        /* ??????????? */
  255.     for (j = 0; buf[i] != EOS; j++) {   /* move line to left */
  256.         buf[j] = buf[i];
  257.         i++;
  258.     }
  259.     buf[j] = EOS;
  260. }
  261.  
  262. /*
  263.  * ngetln - get next line from f into line
  264.  *
  265.  */
  266. int
  267. ngetln(line, f)
  268. char line[];
  269. FILE *f;
  270. {
  271.     int c, i;
  272.  
  273. #ifdef DEBUG
  274.   printf("ngetln bp = %d  f = %d\n", bp, f);
  275. #endif
  276.     for (i = 0; (c = (bp >= 0) ? buf[bp--] : getc(f)) != EOF; ) {
  277.         if (i < MAXLINE - 1) {
  278.             line[i++] = (char) c;
  279.         }
  280.         if (c == '\n' || c == '\r')
  281.             break;
  282.     }
  283.     line[i] = EOS;
  284.     if (i == 0 && c == EOF)
  285.         i = EOF;
  286. #ifdef DEBUG
  287.     printf("ngetln: %s returns %d (line)\n", line, i);
  288. #endif
  289.     return(i);
  290. }
  291.  
  292. /*
  293.  * pbstr - push string back onto input
  294.  *
  295.  */
  296. pbstr(in)
  297. char in[];
  298. {
  299.  
  300.     int i;
  301.  
  302.     dprintf("pbstr  ");
  303.     for (i = strlen(in) - 1; i >= 0; i--)
  304.         putbak(in[i]);
  305. }
  306.  
  307. /*
  308.  * pfoot - put out page footer
  309.  *
  310.  */
  311. pfoot()
  312. {
  313.  
  314.     dprintf("pfoot  ");
  315.     skipl(m3val);
  316.     if (m4val > 0) {
  317.         if (curpag % 2 == 1)
  318.             puttl(efoot, eflim, curpag);
  319.         else
  320.             puttl(ofoot, oflim, curpag);
  321.     }
  322.     if (print == YES)        /* flush the page */
  323.     {
  324.         putchar(PAGEJECT);    /* ...          */
  325.         p_outpages++;
  326.         if (stopx > 0)        /* -s, so flush ^L*/
  327.             putchar('\n');
  328.     }
  329. }
  330.  
  331. /*
  332.  * phead - put out page header
  333.  *
  334.  */
  335. phead()
  336. {
  337.     dprintf("phead  ");
  338.  
  339.     curpag = newpag;
  340.     if (curpag >= frstpg && curpag <= lastpg)
  341.         print = YES;
  342.     else 
  343.         print = NO;
  344.     if(stopx > 0 && print == YES)
  345.         prmpt(&stopx);
  346.     newpag++;
  347.     if (m1val > 0) {
  348.         skipl(m1val-1);
  349.         if (curpag % 2 == 0)
  350.             puttl(ehead, ehlim, curpag);
  351.         else
  352.             puttl(ohead, ohlim, curpag);
  353.     }
  354.     skipl(m2val);
  355.     lineno = m1val + m2val + 1;
  356. }
  357.  
  358. /*
  359.  * prmpt - pause for paper insertion
  360.  * prompt if i == 1; increment i
  361.  *
  362.  */
  363. prmpt(i)
  364. int *i;
  365. {
  366.     int junk,j;
  367.     static char bellst[2] = { BEL, EOS};
  368.  
  369.     dprintf("prmpt  ");
  370.     j = *i;
  371.     if (j == 1)
  372. #ifdef rainbow
  373.         printf("%s\033[7minsert paper and type return\033[0m ",bellst);
  374. #else
  375.         printf("%sinsert paper and type return ",bellst);
  376. #endif
  377.     else
  378.         printf(bellst);
  379.     getchar();
  380.     *i = ++j;
  381. }
  382.  
  383. /*
  384.  * Put - put out line with proper spacing and indenting
  385.  *
  386.  */
  387. put(buf)
  388. char buf[];
  389. {
  390.     register int i;
  391.     dprintf("put  ");
  392.     if (lineno == 0 || lineno > bottom)
  393.         phead();
  394.  
  395.     if ( print == YES ) {
  396.         if (buf[0] == '\n') {    /* empty line.. */
  397.             putchar('\n');
  398.             p_outlines++;
  399.         }
  400.         else {
  401.             for ( i = 1 ; i <= offset ; i++ ) /* page offset */
  402.                 putchar(' ');
  403.             for ( i = 1 ; i <= tival ; i++ )  /* indenting   */
  404.                 putchar(' ');
  405.  
  406.             while (*buf != '\0') {
  407.                 putchar(*buf);
  408.                 buf++;
  409.             }
  410.             p_outlines++;
  411.         }
  412.     }
  413.  
  414.     tival = inval;
  415.     skipl(((lsval-1 < bottom-lineno) ? lsval-1 : bottom-lineno));
  416.     lineno += lsval;
  417.  
  418.     if (lineno > bottom)
  419.         pfoot();
  420.  
  421. }
  422.  
  423. /*
  424.  * putbak - push character back onto input
  425.  *
  426.  */
  427. putbak(c)
  428. char c;
  429. {
  430.     dprintf("putbak  ");
  431.  
  432.     bp++;
  433.     if (bp > BUFSIZE)
  434.         error("too many characters pushed back.\n");
  435.     buf[bp] = c;
  436. }
  437.  
  438.  
  439. /*
  440.  * puttl - put out title line with optional page number & date
  441.  * 
  442.  */
  443. puttl(buf, lim, pageno)
  444. char buf[];
  445. int lim[];
  446. int pageno;
  447. {
  448.     char chars[9],cdate[27];
  449.     char rmstr[MAXTOK];
  450.     char delim;
  451.     char *tp;
  452.     int j;
  453.     int nc, n, i, left, right, ncd;
  454.  
  455.     dprintf("puttl  ");
  456.     if (print == NO)
  457.         return;
  458.     left = lim[0];    /* no more +1 here */
  459.     right = lim[1]; /* no more +1 here */
  460.     nc = itoc(pageno, chars, MAXCHARS);
  461.     if (roman) {
  462.         nc = cvtroman(chars,rmstr);
  463.         strcpy(chars,rmstr);
  464.     }
  465.     getnow(cdate);
  466.     ncd = strlen(cdate);
  467.     i = 0;
  468.     delim = buf[i];
  469.     for (j = 0; j < right; j++)
  470.         ttl[j] = ' ';
  471.     n = 0;
  472.     do {
  473.         if (gfield(buf, &i, right-left, tbuf1, delim) > 0) {
  474.             subst(tbuf1, PAGENUM, tbuf2, chars, nc);
  475.             subst(tbuf2, CURRENTDATE, tbuf1, cdate, ncd);
  476.             justfy(tbuf1, left, right, tjust[n], ttl);
  477.         }
  478.         n++;        /* update title counter */
  479.     } 
  480.     while (buf[i] != EOS && buf[i] != '\n' && n != 3);
  481.  
  482.     for( ; right >= 1 ; right--)
  483.         if( ttl[right-1] != ' ' )
  484.             break;
  485.     ttl[right] = '\n';
  486.     ttl[right+1] = EOS;
  487.     for (i = 1; i <= offset; i++)
  488.         putchar(' ');              /* offset */
  489.     tp = ttl;
  490.     while (*tp != '\0') {
  491.         putchar(*tp);
  492.         tp++;
  493.     }
  494.     p_outlines++;
  495. }
  496.  
  497. /*
  498.  * set - set parameter and check range
  499.  *
  500.  */
  501. set(param, val, argtyp, defval, minval, maxval)
  502. int *param;
  503. int val;
  504. int argtyp;
  505.